package com.mall.file.configuration;

import lombok.Data;
import org.javaswift.joss.client.factory.AccountConfig;
import org.javaswift.joss.client.factory.AccountFactory;
import org.javaswift.joss.client.factory.AuthenticationMethod;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.model.Container;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description:
 * @author: ljr
 * @time: 2021/9/5 10:39
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "ceph")
public class ContainerConfig {

    private String userName;

    private String password;

    private String authUrl;

    private String defaultContainerName;

    /**
     * @description:  创建账号信息
     * @author: ljr
     * @time: 2021/9/5 11:11
     * @return: org.javaswift.joss.model.Account
     */
    @Bean
    public Account account(){
        AccountConfig accountConfig = new AccountConfig();
        accountConfig.setUsername(userName);
        accountConfig.setPassword(password);
        accountConfig.setAuthUrl(authUrl);
        accountConfig.setAuthenticationMethod(AuthenticationMethod.BASIC);
        return new AccountFactory(accountConfig).createAccount();
    }

    @Bean
    public Container container(){
       Container container =  account().getContainer(defaultContainerName);
       if(!container.exists()){
           return  container.create();
       }
       return container;
    }

}
